<?php
/**
 * File: Check.class.php
 * Author: camy <375766145@qq.com>
 * Time: 2018/3/24 10:05
 */

namespace Common\Service;


class Check extends Base
{
    /**
     * 生成审核表序列号
     * @return mixed
     */
    protected function uniqueSerial()
    {
        $model = M('checkWork');
        $closure = function () {
            return generate_unique_serial('SH');
        };
        return $this->uniqueKey($model, 'serial', $closure);
    }

    /**
     * 生成审核流程
     * @param $checkWorkId
     * @param $checkUsers
     * @return bool|string
     */
    protected function createCheckWorkFlow($checkWorkId, $checkUsers)
    {
        if (empty($checkUsers) || !is_array($checkUsers)) {
            return false;
        }
        //审核人数
        $userNumber = count($checkUsers);
        for ($i = 0; $i < $userNumber; $i++) {
            $checkUsers[$i]['check_work_id'] = $checkWorkId;
            //第一位-待审核
            if ($i == 0) {
                $checkUsers[$i]['status'] = 1;
            } else {
                $checkUsers[$i]['status'] = 0;
            }
            //最后一位
            if ($i != ($userNumber - 1)) {
                $checkUsers[$i]['next_user_id'] = $checkUsers[$i + 1]['user_id'];
            } else {
                $checkUsers[$i]['next_user_id'] = 0;
            }
            //审核序号
            $checkUsers[$i]['number'] = $i + 1;
        }
        //通知第一位审核人
        $msgParam['user_id'] = $checkUsers[0]['user_id'];
        $msgParam['id'] = $checkWorkId;
        notification('check', 'apply', $msgParam);
        return M('checkWorkFlow')->addAll($checkUsers);

    }

    /**
     * 申请项目积分
     * @param $projectId
     * @param $type
     * @param $points
     * @param $checkUsers
     * @return mixed
     */
    public function applyPointsCheck($projectId, $type, $points, $checkUsers)
    {
        $data['serial'] = $this->uniqueSerial();
        if ($type == 1) {
            $data['type'] = 11;
        } else {
            $data['type'] = 10;
        }
        $data['apply_user_id'] = user()->id;
        $json['project_id'] = $projectId;
        $json['type'] = $type;
        $json['points'] = $points;
        $data['json_data'] = json_encode($json);
        $data['relation_table'] = 'project';
        $data['relation_id'] = $projectId;
        try {
            M()->startTrans();
            $returnId = M('checkWork')->add($data);
            if (!$returnId) {
                throw new \Exception();
            }
            $res = $this->createCheckWorkFlow($returnId, $checkUsers);
            if (!$res) {
                throw new \Exception();
            }
            M()->commit();
            return $returnId;
        } catch (\Exception $e) {
            M()->rollback();
            return false;
        }

    }

    /**
     * 申请项目附件
     * @param $projectId
     * @param $type
     * @param $name
     * @param $attachments
     * @param $checkUsers
     * @return bool|mixed
     */
    public function applyAttachmentCheck($projectId, $type, $name, $attachments, $checkUsers)
    {
        $data['serial'] = $this->uniqueSerial();
        $data['type'] = 12;
        $data['apply_user_id'] = user()->id;
        $json['project_id'] = $projectId;
        $json['type'] = $type;
        $json['name'] = $name;
        $json['attachments'] = $attachments;
        $data['json_data'] = json_encode($json);
        $data['relation_table'] = 'project';
        $data['relation_id'] = $projectId;
        try {
            M()->startTrans();
            $returnId = M('checkWork')->add($data);
            if (!$returnId) {
                throw new \Exception();
            }
            $res = $this->createCheckWorkFlow($returnId, $checkUsers);
            if (!$res) {
                throw new \Exception();
            }
            M()->commit();
            return $returnId;
        } catch (\Exception $e) {
            M()->rollback();
            return false;
        }

    }

    /**
     * 审核通过
     * @param $id
     * @return mixed
     */
    public function confirm($id)
    {
        $flow = M('checkWorkFlow')->find($id);
        //存在下一位审核人
        if ($flow['next_user_id'] != 0) {
            //修改状态-已通过
            M('checkWorkFlow')
                ->where(['id' => $id])
                ->save(['status' => 2]);
            //修改下一位状态-通知下一位审核人
            $number = $flow['number'] + 1;
            M('checkWorkFlow')
                ->where(['check_work_id' => $flow['check_work_id'], 'number' => $number])
                ->save(['status' => 1]);
            $msgParam['user_id'] = $flow['next_user_id'];
            $msgParam['id'] = $flow['check_work_id'];
            notification('check', 'apply', $msgParam);
            return 1;
        } else {
            //审核结束
            return true;
        }
    }

    /**
     * 审核不通过
     */
    public function refuse($id, $reason)
    {

    }


}